home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr43 / xlibp202.zip / XBM2.DOC < prev    next >
Text File  |  1994-06-18  |  20KB  |  438 lines

  1. ╔═══════════════════════════════════════════════════════════════════════════╗
  2. ║                                                                           ║
  3. ║        XLIB v2.0 - Graphics Library for Borland/Turbo Pascal 7.0          ║
  4. ║                                                                           ║
  5. ║               Tristan Tarrant - tristant@cogs.susx.ac.uk                  ║
  6. ║                                                                           ║
  7. ╠═══════════════════════════════════════════════════════════════════════════╣
  8. ║                                                                           ║
  9. ║                                 Credits                                   ║
  10. ║                                                                           ║
  11. ║                             Themie Gouthas                                ║
  12. ║                                                                           ║
  13. ║                            Matthew MacKenzie                              ║
  14. ║                                                                           ║
  15. ║                             Tore Bastiansen                               ║
  16. ║                                                                           ║
  17. ║                                 Andy Tam                                  ║
  18. ║                                                                           ║
  19. ║                               Douglas Webb                                ║
  20. ║                                                                           ║
  21. ║                              John  Schlagel                               ║
  22. ║                                                                           ║
  23. ╠═══════════════════════════════════════════════════════════════════════════╣
  24. ║                                                                           ║
  25. ║           I informally reserve all rights to the code in XLIB             ║
  26. ║       Rights to contributed code is also assumed to be reserved by        ║
  27. ║                          the original authors.                            ║
  28. ║                                                                           ║
  29. ╚═══════════════════════════════════════════════════════════════════════════╝
  30.  
  31. ╔═══════════════════════════════════════════════════════════════════════════╗
  32. ║ DISCLAIMER                                                                ║
  33. ╚═══════════════════════════════════════════════════════════════════════════╝
  34.  
  35.   This library is distributed AS IS. The author/s specifically disclaim any
  36.   responsibility for any loss of profit or any incidental, consequential or
  37.   other damages.
  38.  
  39. ╔═══════════════════════════════════════════════════════════════════════════╗
  40. ║ XBM2 : EXPORTED PROCEDURES AND FUNCTIONS                                  ║
  41. ╚═══════════════════════════════════════════════════════════════════════════╝
  42.  
  43.   This unit implements a set of functions to operate on bitmaps. XLIB2 uses
  44.   three different kinds of bitmaps :
  45.     Planar Bitmaps (PBMs),
  46.     Video Bitmaps   (VBMs),
  47.     Compiled Bitmaps (CBMs).
  48.  
  49. ╔═══════════════════════════════════════════════════════════════════════════╗
  50. ║ PBMs : EXPORTED PROCEDURES AND FUNCTIONS                                  ║
  51. ╚═══════════════════════════════════════════════════════════════════════════╝
  52.  
  53.   PBMs as used by these functions have the following structure:
  54.  
  55.     byte 0                 The bitmap width in bytes (4 pixel groups) range 1..255
  56.     byte 1                 The bitmap height in rows range 1..255
  57.     byte 2..n1             The plane 0 pixels width*height bytes
  58.     byte n1..n2            The plane 1 pixels width*height bytes
  59.     byte n2..n3            The plane 2 pixels width*height bytes
  60.     byte n3..n4            The plane 3 pixels width*height bytes
  61.  
  62.   These functions provide the fastest possible bitmap blts from system ram
  63.   to video and further, the single bitmap is applicable to all pixel
  64.   alignments. The masked functions do not need separate masks since all non
  65.   zero pixels are considered to be masking pixels, hence if a pixel is 0 the
  66.   corresponding screen destination pixel is left unchanged.
  67.  
  68.  
  69.   xputmaskedpbm
  70.   -------------
  71.  
  72.   Procedure xputmaskedpbm( X, Y, ScrnOffs : word; var Bitmap);
  73.  
  74.   Mask write a planar bitmap from system ram to video ram. All zero source
  75.   bitmap bytes indicate destination byte to be left unchanged.
  76.  
  77.   Source Bitmap structure:
  78.  
  79.   Width:byte, Height:byte, Bitmap data (plane 0)...Bitmap data (plane 1)..,
  80.   Bitmap data (plane 2)..,Bitmap data (plane 3)..
  81.  
  82.   NOTE: width is in bytes ie lots of 4 pixels
  83.  
  84.   LIMITATIONS: No clipping is supported
  85.            Only supports bitmaps with widths which are a multiple of
  86.            4 pixels
  87.  
  88.   xputpbm
  89.   -------
  90.  
  91.   Procedure xputpbm( X, Y, ScrnOffs : word; var Bitmap );
  92.  
  93.   Write a planar bitmap from system ram to video ram.
  94.  
  95.   Source Bitmap structure:
  96.  
  97.   Width:byte, Height:byte, Bitmap data (plane 0)...Bitmap data (plane 1)..,
  98.   Bitmap data (plane 2)..,Bitmap data (plane 3)..
  99.  
  100.   NOTE: width is in bytes ie lots of 4 pixels
  101.  
  102.   LIMITATIONS: No clipping is supported
  103.            Only supports bitmaps with widths which are a multiple of
  104.            4 pixels
  105.  
  106.   xgetpbm
  107.   -------
  108.  
  109.   Procedure xgetpbm( X, Y : word; Bw, Bh : byte; ScrnOffs : word;
  110.         var Bitmap );
  111.  
  112.   Read a planar bitmap to system ram from video ram.
  113.  
  114.   Source Bitmap structure:
  115.  
  116.   Width:byte, Height:byte, Bitmap data (plane 0)...Bitmap data (plane 1)..,
  117.   Bitmap data (plane 2)..,Bitmap data (plane 3)..
  118.  
  119.   NOTE: width is in bytes ie lots of 4 pixels
  120.  
  121.   LIMITATIONS: No clipping is supported
  122.            Only supports bitmaps with widths which are a multiple of
  123.            4 pixels
  124.  
  125.  
  126.  
  127.   A similar set of functions have been implemented to operate on planar
  128.   bitmaps but incorporating clipping to a user defined
  129.   clipping rectangle (which is set by xsetcliprect )
  130.  
  131.   There are three variations of the normal functions in this unit
  132.   identified by the three function name extensions: clipx, clipy clipxy.
  133.   Because speed is critical in games programming you do not want to be
  134.   checking for clipping if not necessary thus for sprites that move only
  135.   horizontally you would use the clipx version of the put function,
  136.   for sprites that move vertically you would use the clipy version and for
  137.   sprites that move both directions you would use the clipxy version.
  138.   Keep in mind also that the clipping components of these functions assume
  139.   that the clipping rectangle is equal to or larger than the size of the
  140.   bitmap ie. if a bitmap is top clipped, it is assumed that the bitmap's
  141.   bottom is not also clipped. Similarly with horizontal clipping.
  142.   Note: performance in decreasing order is as follows.
  143.   clipy,clipx,clipxy with masked puts being slower than unmasked puts
  144.  
  145.   Horizontal clipping is performed to byte boundaries (4 pixels) rather than
  146.   pixels. This allows for the fastest implementation of the functions. It is
  147.   not such a handicap because for one, your screen width a multiple of 4
  148.   pixels wide and  for most purposes it is the screen edges that form the
  149.   clipping rectangle.
  150.  
  151.   Following is an example of setting a clipping rectangle to the logical
  152.   screen edges:
  153.  
  154.   xsetcliprect(0,0,ScrnLogicalByteWidth,ScrnLogicalHeight)
  155.  
  156.   NOTE: the functions now return a value;
  157.     1 if clipped image is fully clipped (ie no portion of it
  158.     appears on the screen) otherwise it returns 0
  159.  
  160.  
  161.   xputpbmclipx
  162.   ------------
  163.   xputpbmclipy
  164.   ------------
  165.   xputpbmclipxy
  166.   -------------
  167.   xputmaskedpbmclipx
  168.   ------------------
  169.   xputmaskedpbmclipy
  170.   ------------------
  171.   xputmaskedpbmclipxy
  172.   -------------------
  173.  
  174.   For a detailed description of parameters etc. see equivalent functions
  175.   xputpbm, xputmaskedpbm
  176.  
  177. ╔═══════════════════════════════════════════════════════════════════════════╗
  178. ║ CBMs : EXPORTED PROCEDURES AND FUNCTIONS                                  ║
  179. ╚═══════════════════════════════════════════════════════════════════════════╝
  180.  
  181.   o  xcompilebitmap compiles your bitmap into native code which writes
  182.      to the VGA screen in an X mode.
  183.   o  xputcbitmap converts X and Y coordinates into a location on the
  184.      screen, sets up the necessary VGA registers, and executes the compiled
  185.      bitmap as a subroutine.
  186.   o  xsizeofcbitmap takes a planar bitmap and returns an integer equal to
  187.      the size of the compiled bitmap which the planar bitmap would produce.
  188.      It is essentially a lobotomized version of xcompilebitmap, with all
  189.      the code generation replaced with a size counter.
  190.  
  191.   xcompilebitmap scans through a source bitmap and generates 8086
  192. instructions to plot every nonzero pixel.  It is designed to be used
  193. before the action begins rather than on-the-fly.  The compiled bitmap
  194. contains no branches, and no reference to the zero (transparent) pixels.
  195. Where two pixels are exactly four columns apart they are plotted with a
  196. single 16-bit store, and the VGA MAPMASK register will be set at most
  197. four times.  As a result your bitmap may run several times faster than a
  198. traditional memory-to-VGA masked blit routine.
  199.   There is no way to perform clipping on these bitmaps, or to plot a
  200. pixel of color zero.
  201.   xcompilebitmap works with bitmaps in the standard Xlib planar bitmap
  202. format.  On a time scale of 60 frames per second, it is actually relatively
  203. slow.  Since a compiled bitmap is relocatable you may just want to have it
  204. saved to disk, and not include the source bitmap in your program at all.
  205.   The source bitmap format is an array of bytes, a little like this:
  206.  
  207.   eye : array[0..29] of byte =
  208.   ( 4, 7,  { four byte columns across, seven rows tall }
  209.   0, 0, 0, 0, 9, 1, 1, 1, 9, 0, 0, 0, 0, 0, 0, 0,
  210.   0, 0, 9, 9, 1, 1, 1, 4, 4, 9, 9, 0, 0, 0, 0, 0,
  211.   0, 9, 9, 1, 2, 0, 0, 4, 4, 1, 9, 9, 0, 0, 0, 0,
  212.   9, 9, 9, 1, 0, 0, 0, 0, 1, 1, 9, 9, 9, 0, 0, 0,
  213.   0, 9, 9, 1, 2, 0, 0, 2, 1, 1, 9, 9, 0, 0, 0, 0,
  214.   0, 0, 9, 9, 1, 1, 1, 1, 1, 9, 9, 0, 0, 0, 0, 0,
  215.   0, 0, 0, 0, 9, 1, 1, 1, 9, 0, 0, 0, 0, 0, 0, 0 );
  216.  
  217.   This is actually a linear bitmap, which is the wrong format for
  218. compilation, but is easier on human eyes.
  219.   To compile this image for a mode 360 pixels (90 byte columns) across:
  220.  
  221. var
  222.   planareye : array[0..29] of byte;
  223.   CompiledEye : pointer;
  224.  
  225. xbmtopbm( eye, planareye);
  226. getmem( CompiledEye, xsizeofcbitmap(planareye));
  227. xcompilebitmap(90, planareye, CompiledEye^);
  228.  
  229.   Notice that both buffers must exist beforehand.  Since xcompilebitmap
  230. returns the size of the compiled code, in bytes, you can reallocate the
  231. bitmap immediately to the right size if using xsizeofxbitmap seems
  232. inconvenient (reallocation may even be faster, though using the function is
  233. cleaner).  The pointers are 32-bit because compiled bitmaps take so much
  234. space: they are at one end of the speed-versus-memory spectrum.  A good
  235. rule of thumb is to allocate (3.5 x buffer-height x buffer-width) + 25
  236. bytes (rounding up ;-), then pare your bitmap down when you find out how
  237. much space you've actually used.
  238.   Since the compiled bitmap has to fit within one segment of memory, it
  239. cannot contain more than about 19,000 pixels.  This will not be a
  240. limitation for most sane programmers.  If you are not a sane programmer try
  241. splitting your huge, unwieldy image up into smaller parts -- you can use
  242. the same gigantic bitmap if you divide it into horizontal slices for
  243. compilation.  For that matter, dividing the source up that way will let
  244. you use a source bitmap large than 64K, which is an even sicker idea...
  245.   Back to business.  A bitmap is compiled for only one width of screen.
  246. If you are using a logical screen larger than your physical screen, call
  247. the bitmap compiler with the logical width -- the important thing is the
  248. number of bytes per line.  Notice that you do not have to be in a graphics
  249. mode to use this routine.  This allows you to develop and compile bitmaps
  250. separately, with whatever utility programs you might cook up.
  251.  
  252.   The final function is xputcbitmap.  To plot our eye at (99,4), on
  253. the page which starts at location 0:
  254. xputcbitmap(99, 4, 0, CompiledEye);
  255.   This function depends on the global variable ScrnLogicalByteWidth which
  256. should be the same number as the column parameter you used to compile your
  257. bitmap.
  258.   The XBM2 unit supports memory-to-VGA blits only.  XBM2 also includes 
  259. non-masking routines which can quickly save and restore the background 
  260. screen behind your bitmap, using fast string operations.
  261.  
  262. xcompilepbm
  263. -----------
  264. xsizeofcpbm
  265. -----------
  266.  
  267. These two procs are similar to the previous two, though they work on PBMs.
  268. There is also a set of procs that produces 32bit bitmaps. These are slightly
  269. larger and slower (why?), but they are there for compatibility reasons.
  270.  
  271. ╔═══════════════════════════════════════════════════════════════════════════╗
  272. ║ VBMs : EXPORTED PROCEDURES AND FUNCTIONS                                  ║
  273. ╚═══════════════════════════════════════════════════════════════════════════╝
  274.  
  275. Yet another type of bitmap to complement planar and compiled bitmaps are
  276. Video Bitmaps, VRAM based bitmaps. If a 4 cylinder car is analagous to planar
  277. bitmaps, that is thrifty on memory consumption but low performance and and a
  278. V8 is analagous to Compiled bitmaps, memory guzzlers that really fly, then
  279. VRAM based bitmaps are the 6 cylinder modest performers with acceptable memory
  280. consumption. They are faster, though, only on VLB/PCI video boards.
  281.  
  282. To summarise their selling points, VBM's are moderately fast with fair memory
  283. consumption, and unlike compiled bitmaps, can be clipped. The disadvantages
  284. are that they are limited by the amount of free video ram and have a complex
  285. structure.
  286.  
  287. The VRAM bitmap format is rather complex consisting of components stored in
  288. video ram and components in system ram working together. This complexity
  289. necessitates the existence of a creation function xmakevbm which takes
  290. an input linear bitmap and generates the equivalent VBM (VRAM Bit Map).
  291.  
  292. VBM structure:
  293.  
  294.   word  0   Size          Total size of this VBM structure in bytes
  295.   word  1   ImageWidth    Width in bytes of the image (for all alignments)
  296.   word  2   ImageHeight   Height in scan lines of the image
  297.  
  298.   word  3 Alignment 0  ImagePtr   Offset in VidRAM of this aligned image
  299.   +--word  4              MaskPtr    Offset (within this structure's DS) of
  300.   |   .                               alignment masks
  301.   |   .
  302.   |   .
  303.   |  word  9 Alignment 3  ImagePtr   Offset in VidRAM of this aligned image
  304.   +|--word 10              MaskPtr    Offset (within this structure's DS) of
  305.   ||                                   alignment masks
  306.   ||
  307.   |+->byte 21 (word 11)                -------+-- Image masks for alignment 0
  308.   |   .                                       |
  309.   |   .                                       |
  310.   |   byte  21 + ImageWidth*ImageHeight  -----+
  311.   |
  312.   |   .
  313.   |   . (similaly for alignments 1 - 2 )
  314.   |   .
  315.   |
  316.   +-->byte  21 + 3*ImageWidth*ImageHeight + 1-+-- Image masks for alignment 3
  317.   .                                       |
  318.   .                                       |
  319.   byte  21 + 4*(ImageWidth*ImageHeight) --+
  320.   .
  321.   .
  322.   << Similarly for alignments 2 and 3 >>
  323.   .
  324.   .
  325.   byte 21 + 4*(ImageWidth*ImageHeight)
  326.   -------------
  327.   (And dont forget the corresponding data in video ram)
  328.  
  329. You can see for yourself the complexity of this bitmap format. The image
  330. is stored in video ram in its 4 different alignments with pointers to these
  331. alignments in the VBM. Similarly there are 4 alignments of the corresponding
  332. masks within the VBM itself (towards the end). The mask bytes contain the
  333. plane settings for the corresponding video bytes so that one memory move can
  334. move up to 4 pixels at a time (depending on the mask settings) using the
  335. VGA's latches, theoretically giving you a 4x speed improvement over
  336. conventional blits like the ones implemented in XPBITMAP. In actual fact
  337. its anywhere between 2 and 3 due to incurred overheads.
  338.  
  339. These bitmaps are more difficult to store in files than PBM'S and CBM's but
  340. still posible with a bit of work, so do not dismiss these as too difficult
  341. to use. Consider all the bitmap formats carefully before deciding on which
  342. to use. There may even be situations that a careful application of all three
  343. types would be most effective ie. compiled bitmaps for Background tiles and
  344. the main game character (which never need clipping), VRAM based bitmaps for
  345. the most frequently occuring (oponent, alien etc) characters which get
  346. clipped as they come into and leave your current location and planar bitmaps
  347. for smaller or less frequently encountered characters.
  348.  
  349.   xmakevbm
  350.   --------
  351.  
  352.   function xmakevbm( var lbm; var VramStart : word) : pointer;
  353.  
  354.   Create the VBM from the given linear bitmap and place the image alignments
  355.   in video ram starting at the offset in the variable pointed to by
  356.   VramStart. VramStart is then updated to point to the next free VRAM byte
  357.   (just after the last byte of the image alignments). Usually you will point
  358.   VramStart to NonVisualOffs.
  359.  
  360.     lbm         Pointer to the input linear bitmap
  361.     VramStart   Pointer to variable containing Offset of first free VRAM byte
  362.  
  363.     xputmaskedvbm
  364.     -------------
  365.  
  366.     function xputmaskedvbm( X, Y, ScrnOffs : word; var VBitmap) : integer;
  367.  
  368.     Draw a VRAM based bitmap at (X,Y) relative to the screen with starting
  369.     offset ScrnOffs.
  370.  
  371.     Returns 1 if clipped image is fully clipped (ie no portion of it
  372.     appears on the screen) otherwise it returns 0
  373.  
  374.     xputmaskedvbmclipx
  375.     ------------------
  376.     xputmaskedvbmclipy
  377.     ------------------
  378.     xputmaskedvbmclipxy
  379.     -------------------
  380.  
  381.     Clipping versions of xputmaskedvbm.
  382.  
  383. ╔═══════════════════════════════════════════════════════════════════════════╗
  384. ║ XBM2 : EXTRA PROCEDURES AND FUNCTIONS                                     ║
  385. ╚═══════════════════════════════════════════════════════════════════════════╝
  386.  
  387.   LINEAR BITMAPS
  388.   
  389.   Linear bitmaps have the following structure:
  390.  
  391.   byte 0                 The bitmap width in pixels  range 1..255
  392.   byte 1                 The bitmap height in rows   range 1..255
  393.   byte 2..n              The width*height bytes of the bitmap
  394.  
  395.   xpbmtobm
  396.   --------
  397.   function xpbmtobm( var sourcepbm, destbm ) : integer;
  398.  
  399.   This function converts a bitmap in the planar format to the linear format
  400.   as used by xcompilebitmap.
  401.  
  402.   WARNING: the source and destination bitmaps must be pre - allocated
  403.  
  404.   NOTE: This function can only convert planar bitmaps that are suitable.
  405.   If the source planar bitmap's width (per plane) is >= 256/4
  406.   it cannot be converted. In this situation an error code
  407.   BMWIDTHERROR. On successful conversion 0 is returned.
  408.  
  409.   xbmtopbm
  410.   --------
  411.   function xbmtopbm( var sourcepbm, destbm ) : integer;
  412.  
  413.   This function converts a bitmap in the linear format as used by
  414.   xcompilebitmap to the planar formap.
  415.  
  416.   WARNING: the source and destination bitmaps must be pre - allocated
  417.  
  418.   NOTE: This function can only convert linear bitmaps that are suitable.
  419.   If the source linear bitmap's width is not a multiple of 4
  420.   it cannot be converted. In this situation an error code
  421.   BMWIDTHERROR. On successful conversion 0 is returned.
  422.  
  423.   xscale
  424.   ------
  425.   Procedure XScale( DestX, DestY, DestWidth, DestHeight, ScrnOffs : word;
  426.         var Bitmap );
  427.  
  428.   xmaskedscale
  429.   ------------
  430.   Procedure XMaskedScale( DestX, DestY, DestWidth, DestHeight, ScrnOffs : word;
  431.         var Bitmap );
  432.  
  433.   These two procedures put a linear bitmap in VRAM, scaling it to DestWidth
  434.   and DestHeight size, at position DestX,DestY at offset ScrnOffs.
  435.   I have tried to write similar functions to work with PBMs, but they are
  436.   slower, because of the way PBMs are organised. If you have a PBM you want to
  437.   scale, just convert it to an LBM using pbmtobm.
  438.